# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao_v2.invoiceDao import InvoiceDao

from . import app

CORS(app, supports_credentials=True)


@app.route('/insertInvoice', methods=['POST'])
def insert_invoiceNoToken():
    """
    添加信息
    :return:
    """
    if not request.json:
        abort(400)
    info = {
        'userId': request.json.get('userId', 0),
        'invoiceType': request.json.get('invoiceType', ''),
        'invoiceContent': request.json.get('invoiceContent', ''),
        'invoiceHeader': request.json.get('invoiceHeader', ''),
        'invoiceStatua': request.json.get('invoiceStatua', 0),
        'invoiceRemark': request.json.get('invoiceRemark', '')
    }

    info = InvoiceDao().insertInvoice(info)
    if info:
        return jsonify({"message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/findInvoiceById', methods=['POST'])
def find_invoice_by_idNoToken():
    """
    根据ID查询
    :return:
    """
    if not request.json:
        abort(400)
    invoiceId = request.json.get('invoiceId', '')
    info = InvoiceDao().findInvoiceById(invoiceId)

    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/findInvoiceByCondition', methods=['POST'])
def find_invoice_by_conditionNoToken():
    """
    条件查询
    :return:
    """
    if not request.json:
        abort(400)

    userId = request.json.get('userId', '')
    info = InvoiceDao().findInvoiceByCondition(userId=userId)

    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/deleteInvoiceByIds', methods=['POST'])
def delete_invoice_by_idsNoToken():
    """
    根据ID删除，ID以数组形式传递
    :return:
    """
    if not request.json:
        abort(400)
    idArray = request.json.get('idArray', [0])

    info = InvoiceDao().deleteInvoice(idArray)
    if info:
        return jsonify({"message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/updateInvoiceById', methods=['POST'])
def update_invoice_by_idNoToken():
    """
    更新
    :return:
    """
    if not request.json:
        abort(400)
    try:
        invoId = request.json['invoiceId']
    except:
        return jsonify({"message": {"code": 0, "status": "请传入要修改信息的ID"}})
    oldInfo = InvoiceDao().selectInvoice(invoId)
    info = {
        'invoiceId': request.json['invoiceId'],
        'userId': request.json.get('userId', oldInfo[1]),
        'invoiceType': request.json.get('invoiceType', oldInfo[2]),
        'invoiceContent': request.json.get('invoiceContent', oldInfo[3]),
        'invoiceHeader': request.json.get('invoiceHeader', oldInfo[4]),
        'invoiceStatua': request.json.get('invoiceStatua', oldInfo[5]),
        'invoiceRemark': request.json.get('invoiceRemark', oldInfo[6])
    }

    info = InvoiceDao().updateInvoice(info)
    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})
